<?php

namespace App\Http\Controllers\Api;

use App\Client;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use SebastianBergmann\CodeCoverage\TestFixture\C;

class ClientController extends Controller
{
    public function index(){
        $this->is_admin();
        $clients = Client::all();
        return $this->success($clients);
    }
    public function show(Client $client){
        $this->is_admin();
        return $this->success($client);
    }
    public function create(Request $request){
        $this->is_admin();
        $validate = $this->validate($request,[
            'corporate_name'=>'required',
            'firstname'=>'required',
            'lastname'=>'required',
            'phone'=>'required|max:20',
            'email'=>'required|email'
        ]);

        $validator = Validator::make($request->all(),['corporate_name'=>'unique:clients']);
        if ($validator->fails()) return $this->error_401('This Corporate name has already been token');

        $client = new Client($validate);
        $client->save();

        return $this->success(['id'=>$client->id]);
    }

    public function delete(Client $client){
        $this->is_admin();
        if ($client->events->count() > 0) return $this->error_422('Client still has already events');
        $client->delete();
        return $this->success();
    }

}
